package paleGoldenRod.client;


import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.rpc.ServiceDefTarget;
import com.google.gwt.user.client.ui.KeyboardListener;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class GWTGUI implements EntryPoint {

	private final class JoinCallback implements AsyncCallback {
		int instance;
		public JoinCallback(int length) {
			instance=length-1;
		}

		public void onFailure(Throwable caught) {
		      // TODO Auto-generated method stub
		      
		 }

		public void onSuccess(Object result) {
		      String suggestions = (String) result;
				//String printedSuggestions = new String();
				label[0][0].setText("Received: " + suggestions);
		 }
/*
		private int min(int length, int length2) {
			if(length>length2)
				return length2;
			else
				return length;
		}
		//*/
	}

	
	private final class NgramCallback implements AsyncCallback {
		int instance;
		public NgramCallback(int length) {
			instance=length-1;
		}

		public void onFailure(Throwable caught) {
		      // TODO Auto-generated method stub
		      
		 }

		public void onSuccess(Object result) {
		      //String suggestions = (String) result;
				//String printedSuggestions = new String();
				label[0][0].setText("Success!");
		 }
/*
		private int min(int length, int length2) {
			if(length>length2)
				return length2;
			else
				return length;
		}
		//*/
	}
	/**
	 * This is the entry point method.
	 */
    static MessagingServiceAsync getMessagingService(){
        MessagingServiceAsync predictionService = (MessagingServiceAsync) 
		             GWT.create(MessagingService.class);
        ServiceDefTarget endpoint = (ServiceDefTarget) predictionService;
        String moduleRelativeUrl = GWT.getModuleBaseURL() + "messagingService";
        endpoint.setServiceEntryPoint(moduleRelativeUrl);
        return predictionService;
   }
	private TextBox textbox = new TextBox();
	private Label label[][] = new Label[3][5];
	public void onModuleLoad() {
		textbox.setText("GWTGUI");
		textbox.setWidth("40em");
		textbox.setReadOnly(false);
		textbox.setEnabled(true);
		
		textbox.addKeyboardListener(new KeyboardListener(){

			public void onKeyDown(Widget sender, char keyCode, int modifiers) {
				// TODO Auto-generated method stub
				
			}

			public void onKeyPress(Widget sender, char keyCode, int modifiers) {
				// TODO Auto-generated method stub
				
			}

			public void onKeyUp(Widget sender, char keyCode, int modifiers) {
				// TODO Auto-generated method stub
				if(keyCode==13){
					commit();
				}
				else if(keyCode==32){
					update();
				}
				//handleChange();
			}

			
		});

		// Assume that the host HTML has elements defined whose
		// IDs are "slot1", "slot2".  In a real app, you probably would not want
		// to hard-code IDs.  Instead, you could, for example, search for all 
		// elements with a particular CSS class and replace them with widgets.
		//
		RootPanel.get("top").add(textbox);
		for(int j=0;j<label.length;j++){
			for(int i=0;i<label[j].length;i++){
				label[j][i] = new Label();
				RootPanel.get("word"+(j+1)+""+(i+1)).add(label[j][i]);
			}
		}
		//getPredictions("in the ".split(" "),5);
		
		// dRootPanel.get().add(new WordTrain());
	}
	protected void update() {
		// TODO Auto-generated method stub
        MessagingServiceAsync predictionService = getMessagingService();
        AsyncCallback callback = new JoinCallback(0);
        predictionService.getString(callback);
	}
	protected void commit() {
		// TODO Auto-generated method stub
        MessagingServiceAsync predictionService = getMessagingService();
        AsyncCallback callback = new NgramCallback(0);
        predictionService.sendString(textbox.getText(), callback);
	}
}
